<?php

/**
 * @Author:      lyt
 * @DateTime:    2015-12-29 14:59:14
 * @Description: 用于scs 的门店面板的 信息系统
 */
class ScsPanelController extends BaseController {

    /**
     * @Author:      lyt
     * @DateTime:    2015-12-29 15:05:13
     * @Description: 用来查询门店面板的工位的
     */
    public function stationlist(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ms_arr=DB::table("maintenance_station")->leftJoin("maintenance_station_car_status","maintenance_station_car_status.station_id","=","maintenance_station.id")
	    	->leftJoin("engineer_tokens","engineer_tokens.work_station_num","=","maintenance_station.id")
	    	->leftJoin("maintenance_engineers as me1","me1.id","=","engineer_tokens.engineer_id")
	    	->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","maintenance_station_car_status.car_model_id")
            ->leftJoin("orders","orders.order_id","=","maintenance_station_car_status.order_id")
            ->leftJoin("maintenance_engineers as me2","me2.id","=","orders.engineer_id")
            ->where("maintenance_station.status",1)
            ->where("maintenance_station.type","<>",3)
            ->where("maintenance_station.shop_id",$sc_user->shop_id)
	    	->select("maintenance_station.id as ms_id","maintenance_station.name as ms_name",
                "maintenance_station_car_status.status as ms_status","maintenance_station_car_status.car_no",
                "maintenance_station_car_status.order_id","maintenance_station_car_status.vin_code",
                "maintenance_station_car_status.mileage","maintenance_station_car_status.insurance","maintenance_station_car_status.start_off_time",
                "car_standard_info.model_name","car_standard_info.id as car_model_id",
                "car_standard_info.series_name_id","car_standard_info.series_name", "car_standard_info.brand_name", "car_standard_info.brand_name_id",
                "me1.id as login_me_id","me1.name as login_me_name","me2.id as me_id",
                "me2.name as me_name","orders.name as custom_name","orders.estimated_completion_time")->orderBy("maintenance_station.name","asc")->get();
	    	// foreach ($ms_arr as $key=>$value){
	    	//     $station[$key] = $value->ms_name;
	    	// }
	    	// array_multisort($station,SORT_STRING,SORT_ASC,$ms_arr);
	    	$temp_arr=array("count"=>sizeof($ms_arr),"date"=>Common::num2DateTime(time()),"station"=>$ms_arr);
	    	$response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
    	}while(false);
    	// Log::info("info", array("system" => "scs" ,"event" => "station_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2015-12-30 11:29:23
     * @Description: 门店工位信息
     */
    public function station(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "station_id"=>'required',
            )
        );
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ms_arr=DB::table("maintenance_station")->leftJoin("maintenance_station_car_status","maintenance_station_car_status.station_id","=","maintenance_station.id")
            ->leftJoin("engineer_tokens","engineer_tokens.work_station_num","=","maintenance_station.id")
            ->leftJoin("maintenance_engineers","maintenance_engineers.id","=","engineer_tokens.engineer_id")
            ->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","maintenance_station_car_status.car_model_id")
            ->leftJoin("orders","orders.order_id","=","maintenance_station_car_status.order_id")->
            where("maintenance_station.status",1)->where("maintenance_station.id",Input::get("station_id"))->
            select("maintenance_station.id as ms_id","maintenance_station.name as ms_name","maintenance_station_car_status.status as ms_status","maintenance_station_car_status.car_no","maintenance_station_car_status.order_id","maintenance_station_car_status.vin_code","maintenance_station_car_status.mileage","maintenance_station_car_status.insurance","maintenance_station_car_status.start_off_time","car_standard_info.model_name","car_standard_info.id as car_model_id","car_standard_info.series_name","car_standard_info.brand_name","maintenance_engineers.id as me_id","maintenance_engineers.name as me_name","orders.name as custom_name")->first();
            // $temp_arr=array("count"=>sizeof($ms_arr),"station"=>$ms_arr);
	    	$response = array('code' => 0, 'msg' => 'ok', 'result' => $ms_arr);
    	}while(false);
    	// Log::info("info", array("system" => "scs" ,"event" => "station_show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-01-05 15:22:13
     * @Description: 根据车位获取车位上的车辆信息
     */
    public  function station_carinfo(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "station_id"=>'required|integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msc=MaintenanceStationCar::where("station_id",Input::get("station_id"))->first();//huoqu
            if(is_null($msc)||$msc->status!=1){
                $response = Lang::get("scs_error.work_station_status_status22001");
                break;
            }
            if(!$msc->car_no||!$msc->car_model_id){
                $response = Lang::get("scs_error.work_station_is_empty22002");
                break;
            }
            $csi=DataCarStandardInfo::find($msc->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            $number=Orders::where("car_no",$msc->car_no)->whereIn("status",Orders::$end_status)->max("mileage");
            // $orders=Orders::where("car_no",$msc->car_no)->whereIn("status",Orders::$end_status)->orderBy("mileage",'desc')->get();;
            // $user_car_id=$orders[0]->user_car_id;
            // $uci=UserCarInfo::find($user_car_id);
            $mscs=MaintenanceStationCarStatus::where('shop_id',$sc_user->shop_id)->where('station_id',Input::get("station_id"))->first();
            $temp_arr=array(
                    "car_no"=>$mscs->car_no,
                    "kilometre"=>$mscs->mileage,
                    "befor_kilometre"=>$number,
                    "vin"=>$mscs->vin_code,
                    "desc"=>"",
                    "brand_name"=>$csi->brand_name,
                    "series_name"=>$csi->series_name,
                    "model_name"=>$csi->model_name,
                    "model_id"=>$csi->id,
                    // "insurance"=>isset($uci->insurance) ? $uci->insurance : ''
                    "insurance"=>$mscs->insurance,
                    "start_off_time"=>$mscs->start_off_time,
                );
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>$temp_arr);
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "station_carinfo", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-01-14 10:18:29
     * @Description: 用于展示 停车位上的 订单车辆 信息
     */
    public function parkList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }

            $mp=MaintenancePark::leftJoin("orders","orders.order_id","=","maintenance_park.order_id")
                ->leftJoin('users','users.id','=','orders.user_id')
                ->whereNotIn('orders.status',array(1310,1110,1010,1210,1410))
                ->where("maintenance_park.shop_id",$sc_user->shop_id)
                // ->orderBy('maintenance_park.status','ASC')
                // ->orderBy('orders.estimated_completion_time','ASC')
                // ->orderBy('orders.start_time','ASC')
                ->orderBy(DB::raw("coalesce(orders.start_time, '')"), 'DESC')
                ->select(array("maintenance_park.order_id","maintenance_park.status","orders.car_no","orders.estimated_completion_time",
                    "car_brand_id","car_brand_name","car_series_id","car_series_name","car_model_id","car_model_name",
                    "orders.start_time","orders.engineer_id","orders.user_id","users.type"));
            if(Input::get('status')){
                $status=json_decode(Input::get('status'),1);
                $mp->whereIn('maintenance_park.status',$status);
            }
            if(Input::get('car_brand_id')){
                $mp->where('orders.car_brand_id',Input::get('car_brand_id'));
            }
            if(Input::get('car_no')){
                $mp->where('orders.car_no','like','%'.Input::get('car_no').'%');
            }
            if(Input::get('type')){
                $mp->where('users.type',Input::get('type'));
            }
            $mp=$mp->get();
            foreach($mp as $_mp){
                if($_mp->engineer_id==0){
                    $_mp->engineer_name="";
                }else{
                    $me=MaintenanceEngineers::find($_mp->engineer_id);
                    $_mp->engineer_name=$me->name;
                }
                if($_mp->type==2){
                    $compnay=Companys::find($_mp->user_id);
                    $_mp->company_name=isset($compnay)?$compnay->name:"";
                }else{
                    $_mp->company_name="";
                }
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("count"=>sizeof($mp),"date"=>Common::num2DateTime(time()),"parks"=>$mp));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "parkList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-01-14 16:10:23
     * @Description: 用于预约未到店的接口
     */
    public function inAppointment(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $mp=MaintenancePark::where("shop_id",$sc_user->shop_id)->select("order_id")->get()->toArray();
            $msc=MaintenanceStationCar::where("shop_id",$sc_user->shop_id)->whereNotNull("order_id")->select("order_id")->get()->toArray();
            $ids=array();
            $temp=array_merge($mp,$msc);
            foreach ($temp as $_t) {
               $ids[]=$_t["order_id"];
            }
            $orders=Orders::where("shop_id",$sc_user->shop_id)->where("place",2)->where("order_type",1);
            if(Input::get("car_no")){
                $orders=$orders->where("car_no",Input::get("car_no"));
            }
            $date=date("Y-m-d",time());
            $enddate=date("Y-m-d",time()+7*60*60*24);
            // if(Input::get("date")){
            //     $date=date("Y-m-d",strtotime(Input::get("date")));
            // }
            $count=$orders->whereIn("status",array(1112,1312))->whereBetween("book_time",array($date,$enddate))->whereNotIn("order_id",array_unique($ids))->count();
            
            $final_orders=$orders->whereIn("status",array(1112,1312))->whereBetween("book_time",array($date,$enddate))->whereNotIn("order_id",array_unique($ids))->select(array("order_id","car_no","phone","book_time","book_hour","is_call","call_status","name","note"))
            // ->skip($page_size*($page-1))->take($page_size)
            ->orderBy('is_call','desc')
            ->orderBy('call_status','asc')
            ->get();
            $temp_count=$orders->whereIn("status",array(1112,1312))->whereBetween("book_time",array($date,$enddate))->whereNotIn("order_id",array_unique($ids))->where("is_call",1)->where("call_status",0)->count();
            foreach ($final_orders as $_o) {
                $ot_arr=OrderType::where("order_id",$_o->order_id)->select("order_id","order_type")->get();
                $_o->order_typearr=$ot_arr;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("count"=>$count,"temp_count"=>$temp_count,"orders"=>$final_orders));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "parkList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-20 16:09:30
     * @Description: 车辆服务完成出位
     */
    public function out_station(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->status != 1216){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40024");
                break;
            }
            //修改订单服务结束时间
            $orders->end_time=date("Y-m-d H:i:s",time());
            $orders->save();

            //入停车场记录
            $mp = new MaintenancePark();
            $mp->shop_id=$sc_user->shop_id;
            $mp->order_id=Input::get('order_id');
            $mp->status=1;
            $mp->user_id=$sc_user->id;
            $mp->save();

            //腾出车位
            $mscs=MaintenanceStationCarStatus::where('order_id',Input::get('order_id'))->where('status',1)->first();
            if(is_null($mscs)){
                $response = Lang::get("scs_error.station_shop_error_31010");
                break;
            }
            $mscs=MaintenanceStationCarStatus::where('order_id',Input::get('order_id'))->update(array("car_no"=>"","status"=>0,"car_model_id"=>0,"order_id"=>0,'mileage'=>0,'vin_code'=>'','insurance'=>'','start_off_time'=>''));
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "out_station", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:  2017-04-10 11:05:54
     * @Description: 预约看板的回访
     */
    public function call_back(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
                "status"=>'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->call_status == 1){//订单已经回访 
                $response = Lang::get("scs_error.order_call_status_error_40093");
                break;
            }
            //记录回访记录
            $ocb=new OrderCallBack;
            $ocb->order_id=$orders->order_id;
            $ocb->status=Input::get("status");
            $ocb->create_by=$sc_user->id;
            $ocb->save();
            //判断是否更改回访状态
            if(Input::get("status")==1){
                $orders->call_status=1;
                $orders->save();
            }   

            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "out_station", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-29 13:50:47
     * @Description: 车辆入停车场
     * @link：http://www.activity.com/v1/scs/panel/inPark?token=875887b2a901f93106aeccf16fae2f8b&order_id=15300037457584
     */
    public function inPark(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if(!in_array($orders->status,Orders::$in_park)){//判断是否符合可停入停车场状态
                $response = Lang::get("scs_error.order_status_error_40026");
                break;
            }
            $mc=MaintenancePark::where('order_id',Input::get('order_id'))->get();

            //工位占用详细信息入库(结束时间)
            try {
                $station_start_time = MaintenanceStationDetails::UpdatedDetails(Input::get('order_id'),$sc_user->shop_id);
            }catch(Exception $e){
                Log::error("error", array("system" => "scs" ,"event" => "UpdatedDetails", 'input' =>Input::all() ,"exception" => $e));
            }
//            if(is_null($station_start_time)){
//                $response = Lang::get("sce_error.station_car_start_time_exiet_1001");
//                break;
//            }
//            if($station_start_time == '0'){
//                $response = Lang::get("sce_error.station_car_start_time_exiet_1002");
//                break;
//            }
//            if($station_start_time == '1'){
//                $response = Lang::get("sce_error.station_car_start_time_exiet_1003");
//                break;
//            }
//            if($station_start_time == '4'){
//                $response = Lang::get("sce_error.station_car_start_time_exiet_1004");
//                break;
//            }

            if(count($mc)>0){//判断是否符合可停入停车场状态
                $mscs=MaintenanceStationCarStatus::where('order_id',Input::get('order_id'))->update(array("car_no"=>"","status"=>0,"car_model_id"=>0,"order_id"=>0,'mileage'=>0,'vin_code'=>'','insurance'=>'','start_off_time'=>''));//TODO
                $response = Lang::get("scs_error.park_status_error_31012");
                break;
            }

            //入停车场记录
            $mp = new MaintenancePark();
            $mp->shop_id=$sc_user->shop_id;
            $mp->order_id=Input::get('order_id');
            if(in_array($orders->status,array(1215))){
                $status=4;
            }else if($orders->book_type==1){
                $status=2;
            }else{
                $status=3;
            }
            $mp->status=$status;
            $mp->user_id=$sc_user->id;
            $mp->save();

            //腾出车位
            $mscs=MaintenanceStationCarStatus::where('order_id',Input::get('order_id'))->update(array("car_no"=>"","status"=>0,"car_model_id"=>0,"order_id"=>0,'mileage'=>0,'vin_code'=>'','insurance'=>'','start_off_time'=>''));
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "out_station", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-03-16 18:40:04
     * @Description: 清除 工位的车辆信息  仅限 停车入围 没开工的 用于 停车入位后不做项目的
     */
    public function clearstation(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "station_id"=>'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msc=MaintenanceStationCar::where("shop_id",$sc_user->shop_id)->where("station_id",Input::get("station_id"))->first();
            if($msc->status!=1){
                $response = Lang::get("scs_error.work_station_car_no_empty22005");
                break;
            }
            MaintenanceStationCar::where("shop_id",$sc_user->shop_id)->where("station_id",Input::get("station_id"))->update(array("car_no"=>"","status"=>0,"car_model_id"=>0,"order_id"=>0,'mileage'=>0,'vin_code'=>'','insurance'=>'','start_off_time'=>''));
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("station_id"=>Input::get('station_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "clearstation", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    

    /**
     * @Author:      ZY
     * @DateTime:    2018-05-18:40:04
     * @Description: 工位看外调查询
     */
    public function outsideOrders(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_show' => 'required', //是否显示本店外调车辆
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $city_id = Input::get("city_id");
            $shop_id = Input::get("shop_id");
            $shop_show = Input::get("shop_show","true");

            if ($shop_show == "true") {
                //本店当前未完成订单牌照
                $shop_carno = Orders::select("car_no")->where("order_type",1)->whereNotIn("status",[1117,1217,1317,1417,1018,1219,1419,1010,1110,1210,1310,1410])->where("shop_id",$sc_user->shop_id)
                        ->groupBy("car_no")
                        ->get();

                if ($shop_carno->count()< 0){
                    $response = Lang::get("scs_error.work_car_no_error_22009");
                    break; 
                }

                $carno_arr = "";
                foreach ($shop_carno as $v) {
                    $carno_arr .= "'".$v->car_no."',";
                }
                $carno_arr = trim($carno_arr,",");
            }
            
            //店面未完成订单(外调数据排除当前店铺)

            $sql_1 = "select orders.order_id,orders.shop_id,ms.name as shop_name,orders.status,orders.car_no,orders.created_at,orders.estimated_completion_time from orders left join maintenance_shops as ms on orders.shop_id=ms.id where orders.status not in (1117,1217,1317,1417,1018,1219,1419,1010,1110,1210,1310,1410)  and orders.shop_id <> {$sc_user->shop_id} and order_type=1 ";
            if ($city_id != "") $sql_1 .= " and ms.city_id = $city_id";
            if ($shop_id != "") $sql_1 .= " and orders.shop_id = $shop_id";
            if ($shop_show == "true") $sql_1 .= " and car_no in ($carno_arr) ";
            
            //当日和前推14日开始的并完成订单（外调数据排除当前店铺)
            $before_date= date('Y-m-d 00:00:00', strtotime('-14 days'));
            $sql_2 = "select orders.order_id,orders.shop_id,ms.name as shop_name,orders.status,orders.car_no,orders.created_at,orders.estimated_completion_time from orders left join maintenance_shops as ms on orders.shop_id=ms.id where orders.status in (1117,1217,1317,1417,1018) and orders.shop_id <> {$sc_user->shop_id} and  orders.created_at >'$before_date'and order_type=1 ";
            if ($city_id != "") $sql_2 .= " and ms.city_id = $city_id";
            if ($shop_id != "") $sql_2 .= " and orders.shop_id = $shop_id";
            if ($shop_show == "true") $sql_2 .= " and car_no in ($carno_arr) ";
            $sql =  " select * from ($sql_1 UNION ALL $sql_2) a order by order_id";

            $list = DB::select( $sql);
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("list"=>$list));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "outsideOrders", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zy
     * @DateTime:    2018、05、21
     * @Description: 查找有门店的城市
     */
    public function outsideCitys(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $citys = MaintenanceShops::select("rc.id","rc.city_name")->where("maintenance_shops.status",1)->leftJoin("resource_city as rc","rc.id","=","maintenance_shops.city_id")->groupBy("maintenance_shops.city_id")->whereNotNull("rc.id")->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("list"=>$citys));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "outsideCitys", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zy
     * @DateTime:    2018、05、21
     * @Description: 查找城市所有门店
     */
    public function outsideShops(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $city_id = Input::get("city_id");
            
            $shops = MaintenanceShops::select("name","id")
                    ->where("status",1);
            if ($city_id !="")   $shops->where("city_id",$city_id);
            $shops = $shops->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("list"=>$shops));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "outsideShops", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
}